import objectUtils from "@/utils/object/index.js";

export default objectUtils.freeze({
	params: {
		get: (url) => {
			let arr = url.split("?")
			let obj = {}
			if (arr.length > 1) {
				arr.pop().split("&").forEach((item) => {
					obj[item.split("=")[0]] = item.split("=")[1]
				})
			}
			return obj
		},
		replace: (url, obj) => {
			let arr = url.split("?")
			let params = {}
			if (arr.length > 1) {
				arr.pop().split("&").forEach((item) => {
					params[item.split("=")[0]] = item.split("=")[1]
				})
			}
			Object.keys(obj).forEach((key) => {
				params[key] = obj[key];
			})
			let fullUrl = arr[0];
			Object.keys(params).forEach((key) => {
				fullUrl += (fullUrl.endsWith("?") ? '' : (fullUrl.indexOf("?") === -1 ? "?" : "&")) + key + "=" + params[key]
			})
			return fullUrl;
		},
		append: (url, obj) => {
			Object.keys(obj).forEach((key) => {
				url += (url.endsWith("?") ? '' : (url.indexOf("?") === -1 ? "?" : "&")) + key + "=" + obj[key]
			})
			return url;
		},
		remove: (url, keys = []) => {
			let arr = url.split("?")
			let obj = {};
			let str = url;
			if (arr.length > 1) {
				str = arr[0];
				arr.pop().split("&").forEach((item) => {
					if(keys.indexOf(item.split("=")[0]) === -1){
						obj[item.split("=")[0]] = item.split("=")[1];
					}
				})
				Object.keys(obj).forEach((key, index) => {
					str += (str.indexOf("?") === -1 ? "?" : "&") + key + "=" + obj[key]
				})
			}
			return str;
		}
	}
});
